package example.tallydemo.MyContextMeau;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.PopupWindow;
import android.widget.TextView;

import java.util.ArrayList;

import example.tallydemo.R;


/**
 * Created by tuzha on 2018/2/13.
 */

public class TzhContextMenu
{

    //PopupWindow
    View contentView;
    PopupWindow popupWindow;

    //PopupWindow的listview
    LayoutInflater layoutInflater;
    MyContextListAdapter listAdapter;
    ArrayList<MyContextListItem> listItemsData = new ArrayList<>();

    //添加PopupWindow的菜单项
    public void addContextItem(String option,String value)
    {
        MyContextListItem myContextListItem = new MyContextListItem();
        myContextListItem.option = option;
        myContextListItem.value = value;
        listItemsData.add(myContextListItem);
    }

    // context, 所在Activity
    // anchor, 点中的View
    // xOff, yOff, 偏移量
    public void show(Context context, View anchor, int xOff, int yOff)
    {
        // 创建View
        layoutInflater  = LayoutInflater.from(context);
        contentView = layoutInflater.inflate(R.layout.tzh_context_menu, null);

        // 初始化窗口里的内容
        initContentView();

        // 创建PopupWindow, 宽度恒定，高度自适应
        popupWindow = new PopupWindow(contentView, 400,
                ViewGroup.LayoutParams.WRAP_CONTENT);
        popupWindow.setFocusable(true);
        popupWindow.showAsDropDown(anchor, xOff, yOff);

    }

    //初始化PopupWindow
    private void initContentView()
    {
        listAdapter = new MyContextListAdapter();
        ListView listView = contentView.findViewById(R.id.id_context_listview);
        listView.setAdapter(listAdapter);
        listView.setOnItemClickListener(new AdapterView.OnItemClickListener()
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                MyContextListItem it = (MyContextListItem) listAdapter.getItem(position);
                onContextItemClicked(it);
            }
        });
    }

    // 回调
    public interface OnContextMenuItemClickedListener
    {
        public void onContextMenuItemClicked(String option, String value);
    }
    public OnContextMenuItemClickedListener listener;

    // 点击PopupWindow菜单项
    private void onContextItemClicked(MyContextListItem it)
    {
        // 关闭窗口
        popupWindow.dismiss();

        // 回调
        if(listener != null)
        {
            listener.onContextMenuItemClicked(it.option, it.value);
        }
    }



    //创建PopupWindow的每一项内容
    public static class MyContextListItem
    {
        public String option;
        public String value;
    }
    //创建PopupWindow的适配器
    public class MyContextListAdapter extends BaseAdapter
    {

        @Override
        public int getCount()
        {
            return listItemsData.size();
        }

        @Override
        public Object getItem(int position)
        {
            return listItemsData.get(position);
        }

        @Override
        public long getItemId(int position)
        {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent)
        {
            if (convertView == null)
            {
                convertView = layoutInflater.inflate(R.layout.tzh_context_menu_item,parent,false);
            }
            MyContextListItem myContextListItem = (MyContextListItem)getItem(position);
            ((TextView)convertView.findViewById(R.id.id_menu_item_textView)).setText(myContextListItem.option);
            return convertView;
        }
    }

}
